<?php

namespace app\models;

use Yii;
use Intervention\Image\ImageManagerStatic as Image;

/**
 * This is the model class for table "focuspic".
 *
 * @property integer $id
 * @property string $title
 * @property string $url
 * @property string $link
 * @property integer $create_time
 * @property integer $sort
 */
class Focuspic extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'focuspic';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['create_time'], 'integer'],
            [['title', 'url'], 'string', 'max' => 128],
            [['title'], 'required'],
            [['link'], 'string'],
            [['sort'], 'default','value'=>0],
            ['url' , 'checkscale' , 'skipOnEmpty' => true],
        ];
    }

    public function checkscale($attribute , $params)
    {
        $path = $this->$attribute;
        Image::configure(array('driver' => 'gd'));

        $width = Image::make($path)->width();
        $height = Image::make($path)->height();

        if($width<750) {
            $this->addError($attribute , '图片宽度不能小于750');
        }elseif($width%750!=0 || $height%330!=0) {
            $this->addError($attribute , '图片比例错误,请按照750:330的比例上传图片');
        }

    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'url' => '图片地址',
            'link' => '链接地址',
            'create_time' => '创建时间',
            'sort' => '排序',
        ];
    }

    public function save($runValidation = true, $attributeNames = null)
    {
        if($this->isNewRecord) {
            $this->create_time=time();
        }
        return parent::save($runValidation, $attributeNames);
    }
}
